<template>
    <div class="certifyNeed">
        <div v-if="!isShowData">
            <i class="el-icon-warning"></i>
            <p class="tip">请选择分类，若不存在分类，应首先在左侧分类设置中添加分类信息</p>
        </div>

        <Ope v-if="isShowData"
        addFun="addData"
        delFun="delAllData"
        delMoreFun="delMore"
        delChooseFun="delChoose"
        searchFun="toSearch"
        @addData="addCertifyNeed"
        @delAllData="delAllCertifyNeed"
        @delMore="delMore"
        @delChoose="delChoose"
        @toSearch="toSearch"
        :ifAdd="true"
        :ifDel="true"
        :ifDelChoose="true"
        :ifDelMore="true"
        :ifSearch="false"
        :allSearchKind="allSearchKind"
        ></Ope>

        <Table 
        class="table"
        v-if="isShowData" 
        :tableData="allData" 
        :ifSelection="ifSelection"
        ref="table"
        @showChange="showChange"
        @delCertifyNeed="delCertifyNeed"

        ></Table>

        <el-pagination
        background
        style="margin-top: .5rem"
        layout="prev, pager, next"
        :page-size="dataNum"
        :total="dataTotal"
        @current-change="changePage"
        >
        </el-pagination>
        <Dialog
            ref="Dialog"
            @submit="confirmAdd"
            
        ></Dialog>
        <dialog-edit
            ref="dialogEdit"
            @editCertifyNeed="confirmEdit"
        ></dialog-edit>
        

    </div>
</template>

<script>
import util from '@/util/util.js'
import Table from './Table'
import Ope from 'template/Ope'
import Dialog from './Dialog'
import DialogEdit from './DialogEdit'
import axios from 'axios'

export default {
    components: {
      Table,
      Ope,
      Dialog,
      DialogEdit
    },
    name: 'CertifyNeed',
    data () {
        return {
            keyName: '',
            keyWord: '',
            isShowData: true,
            allData: [],
            reqPromise: null,
            dataTotal: 100,
            page: 1,
            dataNum: 10,
            ifSelection: false,
            allSearchKind: [{
                label: '资料名称',
                value: 'infoName'
            },{
                label: '资料类型',
                value: 'infoType'
            }]
        }
    },
    created () {
        this.listenRoute();
    },
    watch:{
        '$route' (to, from) {
            this.listenRoute();
        }
    },
    computed: {
        
    },
    methods: {
        toSearch (param) {
            this.keyName = param.searchKind;
            this.keyWord = util.getInfoTypeValue(param.searchKeyWord);
            
            this.listenRoute();
        },
        delMore (param) {
            var table = this.$refs.table;
            this.ifSelection = param === '多选删除';
            if (!this.ifSelection) {
                table.resetSelectMore();
            }
            else {

            }
        },
        delChoose () {
            var table = this.$refs.table;
            var select = table.selectMore;
            var selectId = [];
            
            select.forEach((el, index) => {
                selectId.push(el.id);
            });
            if (selectId.length === 0) {
                //没有选中的提示
                this.$message({
                    message: '无选中',
                    type: 'info'
                });
                return;
            }
            this.$confirm('确认删除选中？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$store.commit('showLoading');
                axios.get('/index/CertifyNeed/delSelection', {
                    params: {
                        selectId: JSON.stringify(selectId)
                    }
                }).then((datas) => {
                    var data = datas.data;
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    });
                    this.listenRoute();
                }, (err) => {
                    console.log(err)
                    this.$message.error('删除失败，服务器挂了！');
                    this.listenRoute();
                })
            }).catch((err) => {
               
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            });

        },
        changePage (index) {
            this.page = index;
            this.listenRoute();
        },
        delCertifyNeed (index) {
            this.$store.commit('showLoading');
            var id = this.allData[index].id;
            axios.get('/index/CertifyNeed/delCertifyNeed', {
                params: {
                    id: id
                }
            }).then((datas) => {
                var data = datas.data;
                if (data.status === 0) {
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    })
                }
                this.$store.commit('hiddenLoading');
                this.listenRoute();
                
            }, () => {

            });
        },
        showChange(index) {
         
            this.$refs.dialogEdit.show(this.allData[index]);
        },
        confirmEdit(form) {
            this.$store.commit('showLoading');
            var kindName = this.$route.query.param;
            axios.get('/index/CertifyNeed/editNeed', {
                params: {
                    id: form.id,
                    infoName: form.infoName,
                    kindName: form.belong === 'basic' ? 'all' : kindName,
                    belong: form.belong,
                    infoType: form.infoType,
                    chooseValue: JSON.stringify(form.chooseValue),
                    iconName: form.iconName ? form.iconName : ''
                }
            }).then((datas) => {
                var data = datas.data;
              
                this.$message({
                    message: '修改成功',
                    type: 'success'
                });
                this.listenRoute();
              
            }, (err) => {
                console.log(err)
            });
        },
        confirmAdd(form) {
            var kindName = this.$route.query.param;
            this.$store.commit('showLoading');
            axios.get('/index/CertifyNeed/addNeed', {
                params: {
                    infoName: form.infoName,
                    kindName: form.belong === 'basic' ? 'all' : kindName,
                    belong: form.belong,
                    infoType: form.infoType,
                    chooseValue: JSON.stringify(form.chooseValue),
                    iconName: form.iconName ? form.iconName : ''
                }
            }).then((datas) => {
                var data = datas.data;
                if (data.status === 2) {
                    this.$store.commit('hiddenLoading');
                    this.$message({
                        message: '已存在该认证条件',
                        type: 'warning'
                    });
                    return;
                }
                this.$message({
                    message: '新增成功',
                    type: 'success'
                });
                //重新加载数据
                this.listenRoute();
                console.log(datas)
            }, () => {

            });
            
            
        },
        listenRoute() {
            //监听路由
            
            var route = this.$route;
            var routePath = route.name;
            var param = route.query.param;
            this.$store.commit('showLoading');
            if (param) {
                this.getExpertNeed();
            }
            else {
                this.getBasicNeed();
            }
            
        },
        detalData(allData) {
            for (let i = 0; i < allData.length; i ++) {
                allData[i]['belongLabel'] = util.getInfoBelongLabel(allData[i].belong);
                allData[i]['infoTypeLabel'] = util.getInfoTypeLabel(allData[i].infoType);
                allData[i]['chooseValueString'] = allData[i]['chooseValue'] ? allData[i]['chooseValue'].join(' ') : '无';
            }
            return allData;
        },
        reqPromiseThen() {
            this.reqPromise.then((datas) => {
                
                var data = datas.data;
                if (data.status === 0) {
                    this.dataTotal = data.data.dataTotal;
                    this.allData = this.detalData(data.data.allCertifyNeed);
                    
                }
               
                this.$store.commit('hiddenLoading');
            }, (err) => {
                this.$store.commit('hiddenLoading');
            });
        },
        getBasicNeed () {
            this.$store.commit('showLoading');
            var reqPromise = axios.get('/index/CertifyNeed/getBasicNeed', {
                params: {
                    page: this.page,
                    dataNum: this.dataNum,
                    keyName: this.keyName,
                    keyWord: this.keyWord
                }
            });
            this.reqPromise = reqPromise;
            reqPromise.then((datas) => {
                this.reqPromiseThen();
            }, (err) => {
                console.log(err)
            });
            
        },
        addCertifyNeed () {
            var param = this.$route.query.param;
            this.$refs.Dialog.show(param);
        },
        delAllCertifyNeed () {
            var param = this.$route.query.param;
            var kindName = param ? param : 'all';
            this.$confirm('删除数据后不可恢复，确认删除？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$store.commit('showLoading');
                axios.get('/index/certifyNeed/delAllCertifyNeed', {
                    params: {
                        kindName: kindName
                    }
                }).then((datas) => {
                    this.$message({
                        message: '删除成功！',
                        type: 'success'
                    })
                    this.$store.commit('hiddenLoading');
                    this.listenRoute();
                    console.log(datas)
                }, (err) => {
                    this.$store.commit('hiddenLoading');
                    this.listenRoute();
                    this.$message.error('删除失败！')
                    console.log(err)
                })
            }, (datas) => {
               
                this.$store.commit('hiddenLoading');
            });
        },
        //规定设置数据函数为 getData
        getExpertNeed () {
            var kindName = this.$route.query.param;
            this.$store.commit('showLoading');
            var reqPromise = axios.get('/index/CertifyNeed/getExpertNeed', {
                params: {
                    kindName: kindName,
                    page: this.page,
                    dataNum: this.dataNum,
                    keyWord: this.keyWord,
                    keyName: this.keyName
                }
            });
            this.reqPromise = reqPromise;
            reqPromise.then(() => {
                this.reqPromiseThen();
            }, (err) => {

            });
            
            
        }
    }
}
</script>

<style lang="stylus" scoped>
    .certifyNeed
        width 100%
        height 100%
        overflow hidden
        .tip 
            font-size .3rem
            color gray
        .table
            height 80%
            overflow scroll
    
</style>


